/*
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.
*/

func twoSum(nums []int, target int) []int { // 可能有负数
	// 思路：将nums内的元素转到map中
	// 遍历 nums，计算每一元素与目标值的差值(一个数不能用两次)，判断差值是否在map中
	// 在则返回两个数的下标
	m := make(map[int]int) // key为nums的值，value为nums的下表
	for i, v := range nums {
		m[v] = i
	}

	for i, v := range nums {
		sub := target - v
		if index, ok := m[sub]; ok {
			if i != index { // 一个数不能用两次
				return []int{i, index}
			}
		}
	}
	return nil
}